/*
 * Created on Sat Sep 16 2017
 * @flow
 * Copyright (c) 2017 jianglinjie
 */
'use strict';

import NetRequest from './NetRequest';
import * as HttpMethod from '../constants/HttpMethod';
import * as ResponseType from '../constants/ResponseType';

export default class FormRequest extends NetRequest {
  constructor(url: string,
    urlParam: ?Object, 
    formParam: ?Object, 
    responseType: string = ResponseType.JSON_TYPE) {
    let form = '';

    if (formParam) {
      for (let key of Object.keys(formParam)) {
        let pair = `${encodeURIComponent(key)}=${encodeURIComponent(formParam[key])}`;
        if (form.length) {
          form = form + '&';
        }

        form = form + pair;
      }
    }
        
    super(url, 
      urlParam, 
      form || null, 
      { 'Content-Type': 'application/x-www-form-urlencoded' },  
      HttpMethod.POST, 
      responseType
    );
  }
}